#!/bin/bash

# Checks paper outages and paper jams on printers, changes status message and printer icon accordingly

printer_names=( baskerville cleveland lib_iif lib_1st_flr lib_2nd_flr norris2 read2 inslab2 \
                scilab scilib jrc_email younker2 helpdesk2 duerer gencomp2 rose ccl3_bw )

for printer in ${printer_names[@]}
do

	# Get printer name from $printer string
	printer_name=`echo $printer | tr "[:lower:]" "[:upper:]"`
	
	# Test for out-of-paper messages on any tray
	grep -o "TRAY [0-9] EMPTY" ~/printers/status_pages/$printer
	empty_test=`echo $?`

	grep -o "LOAD TRAY [0-9]" ~/printers/status_pages/$printer
	load_test=`echo $?`

	# Test for jams
        grep -o "JAM" ~/printers/status_pages/$printer
	jam_test=`echo $?`

	# Then, see if we have any paper outs or jams
	if [ $empty_test == 0 ] || [ $load_test == 0 ] || [ $jam_test == 0 ]
	then
		echo 1 > ~/printers/paper_status/"${printer}"_paper_status

		# Depending on the error, change the status message
		if [ $empty_test == 0 ] || [ $load_test == 0 ]
		then
			echo "$printer_name is out of paper" > ~/printers/status_messages/"${printer}"_oo
		fi

		if [ $jam_test == 0 ]
		then
			echo "$printer_name has a paper jam" > ~/printers/status_messages/"${printer}"_oo
		fi
	else
		echo 0 > ~/printers/paper_status/"${printer}"_paper_status
		echo "$printer_name is out-of-order" > ~/printers/status_messages/"${printer}"_oo		
	fi

done

exit 0 
